const path = require('path');

function resolve (dir) {
  return path.join(__dirname, dir);
}
function getDateTime () {
  let d, s;
  d = new Date();
  s = d.getFullYear(); // 取年份
  s += '' + ('0' + (d.getMonth() + 1)).substr(-2);// 取月份
  s += '' + ('0' + d.getDate()).substr(-2); // 取日期
  s += '' + ('0' + d.getHours()).substr(-2); // 取小时
  s += '' + ('0' + d.getMinutes()).substr(-2); // 取分
  return s;
}

process.env.VUE_APP_VERSION = require('./package.json').version + '_' + getDateTime();
process.env.VUE_APP_PROJECT = JSON.stringify({
  name: require('./package.json').name,
  version: require('./package.json').version
})

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
  pages: {
    index: {
      // entry for the page
      entry: 'examples/main.js',
      // the source template
      template: 'public/index.html',
      // output as dist/index.html
      filename: 'index.html',
      // when using title option,
      // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
      title: 'previewImg',
      // chunks to include on this page, by default includes
      // extracted common chunks and vendor chunks.
      // chunks: ['chunk-vendors', 'chunk-common', 'index'],
    },
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'main')
      }
    },
    externals: {
      'vue': 'Vue',
      'element-ui': 'ELEMENT'
    },
  },
  chainWebpack: (config) => {
    config.resolve.alias
      .set("$root", resolve(""))
      .set("$public", resolve("public"))
      .set("$dist", resolve("dist"))
    // 为js添加版本号
    config.output.chunkFilename(`js/[name].${process.env.VUE_APP_VERSION}.js`);
    config.output.filename(`js/[name].${process.env.VUE_APP_VERSION}.js`);
  }
})
